package com.ruoyi.business.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.Exercise;
import com.ruoyi.business.domain.bo.ExerciseBo;
import com.ruoyi.business.domain.bo.PlainBo;
import com.ruoyi.business.domain.vo.PlainVo;
import com.ruoyi.business.mapper.CourseMapper;
import com.ruoyi.business.mapper.ExerciseMapper;
import com.ruoyi.business.utils.ConstPools;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.PlainMapper;
import com.ruoyi.business.domain.Plain;
import com.ruoyi.business.service.IPlainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 计划管理Service业务层处理
 * 
 * @author 吴倩芸
 * @date 2023-03-09
 */
@SuppressWarnings("ALL")
@Service
public class PlainServiceImpl extends ServiceImpl<PlainMapper, Plain> implements IPlainService
{
    @Autowired
    private PlainMapper plainMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ExerciseMapper exerciseMapper;

    /**
     * 查询计划管理
     * 
     * @param id 计划管理主键
     * @return 计划管理
     */
    @Override
    public PlainVo selectPlainById(Long id)
    {
        Plain plain = plainMapper.selectPlainById(id);
        return toPlainVo(plain);
    }

    private PlainVo toPlainVo(Plain plain) {
        PlainVo vo = new PlainVo();
        BeanUtils.copyProperties(plain, vo);
        String[] plainTimes = vo.getPlainTime().split(ConstPools.PLAIN_SPLIT);
        String[] plainContexts = vo.getPlainContext().split(ConstPools.PLAIN_SPLIT);
        vo.setCourse(courseMapper.selectCourseById(plain.getCourseId()));
        for (int i = 0; i < plainTimes.length; i++) {
            String time = plainTimes[i];
            if (time.equals(getWeekday())) {
                ExerciseBo bo = new ExerciseBo();
                bo.setPlainId(plain.getId());
                bo.setUserId(SecurityUtils.getUserId());
                bo.setWeekday(time);
                bo.setStartTime(new Date());
                List<Exercise> exercises = exerciseMapper.selectExerciseList(bo);
                if (exercises == null || exercises.size() == 0) {
                    // 如果没有查到就是还没有签到，那就可以签到
                    vo.addPlainDetails(plainTimes[i], plainContexts[i], true, false, false);
                }else {
                    Exercise exercise = exercises.get(0);
                    if (exercise.getStartTime() != null && exercise.getEndTime() == null) {
                        vo.addPlainDetails(plainTimes[i], plainContexts[i], false, true, false);
                    }else if (exercise.getStartTime() != null && exercise.getEndTime() != null){
                        vo.addPlainDetails(plainTimes[i], plainContexts[i], false, false, true);
                    }
                }
                // 查询当前用户是否已经签到
            }
            vo.addPlainDetails(plainTimes[i], plainContexts[i], false, false, false);
        }
        return vo;
    }

    public String getWeekday() {
        // 获取当前时间
        LocalDateTime endDate = LocalDateTime.now();
        // 获取当前周
        Integer week = endDate.getDayOfWeek().getValue();
        return Integer.toString(week);
    }

    /**
     * 查询计划管理列表
     * 
     * @param plain 计划管理
     * @return 计划管理
     */
    @Override
    public List<PlainVo> selectPlainList(PlainBo plain)
    {
        Long userId = SecurityUtils.getUserId();
        List<Long> roles = roleService.selectRoleListByUserId(userId);
        List<Plain> plains = null;
        if (isRole(roles, ConstPools.ADMIN_ROLE_ID)) {
            //
        }else if (isRole(roles, ConstPools.TRAIN_MAN_ROLE_ID)) {
            plain.setUserId(userId);
        }
        plains = plainMapper.selectPlainList(plain);
        if (null != plain) {
            return plains.stream().map(this::toPlainVo).collect(Collectors.toList());
        }
        return null;
    }

    private Boolean isRole(List<Long> roles, Long roleId) {
        for (Long role : roles) {
            if (role == roleId) {
                return true;
            }
        }
        return false;
    }


    /**
     * 新增计划管理
     * 
     * @param plain 计划管理
     * @return 结果
     */
    @Override
    public int insertPlain(Plain plain)
    {
        plain.setCreateTime(DateUtils.getNowDate());
        return plainMapper.insertPlain(plain);
    }

    /**
     * 修改计划管理
     * 
     * @param plain 计划管理
     * @return 结果
     */
    @Override
    public int updatePlain(Plain plain)
    {
        plain.setUpdateTime(DateUtils.getNowDate());
        return plainMapper.updatePlain(plain);
    }

    /**
     * 批量删除计划管理
     * 
     * @param ids 需要删除的计划管理主键
     * @return 结果
     */
    @Override
    public int deletePlainByIds(Long[] ids)
    {
        return plainMapper.deletePlainByIds(ids);
    }

    /**
     * 删除计划管理信息
     * 
     * @param id 计划管理主键
     * @return 结果
     */
    @Override
    public int deletePlainById(Long id)
    {
        return plainMapper.deletePlainById(id);
    }

    @Override
    public List<PlainVo> selectMyPlainList(PlainBo plain) {
        plain.setUserId(SecurityUtils.getUserId());
        List<Plain> list = plainMapper.selectMyPlainList(plain);
        return list.stream().map(item -> {
            return toPlainVo(item);
        }).collect(Collectors.toList());
    }
}
